package String;

import java.util.ArrayList;
import java.util.List;

public class _22_GenerateParentheses {
    List<String> result = new ArrayList<>();
    //hint:consider the situation that "(" different from ")",so use backtrackting method
    // and handle in 2 different ways
    public List<String> generateParenthesis(int n) {
        generateSub("", 0, 0, n);
        return result;
    }

    public void generateSub(String s,int start, int end, int max) {
        if (s.length() == max * 2) {
            result.add(s);
        } else {
            if (start < max) {
                generateSub(s+"(",start+1,end,max);
            }
            if (end < start) {
                generateSub(s + ")", start, end + 1, max);
            }
        }
    }
}
